package com.zlp.partice.io;

import org.junit.Test;

import java.io.IOException;
import java.net.*;

/**
 * @author: zhulaiping
 * @time: 2022/8/3 16:28
 */
public class UDPDemo1 {


    @Test
    public void sender() throws IOException {
        DatagramSocket socket = new DatagramSocket();
        byte[] data = "我来了".getBytes();
        DatagramPacket packet = new DatagramPacket(data,0, data.length,InetAddress.getByName("127.0.0.1"),9000);
        socket.send(packet);
        socket.close();
    }

    @Test
    public void receiver()throws IOException {
        DatagramSocket socket = new DatagramSocket(9000);
        byte[] data = new byte[100];
        DatagramPacket packet = new DatagramPacket(data,0, data.length);
        socket.receive(packet);
        System.out.println(new String(packet.getData(),0,packet.getLength()));
    }


}
